package linkedList;

import po.ListNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 面试题链表求和0205.java
 * @Description TODO https://leetcode-cn.com/problems/sum-lists-lcci/
 * @createTime 2021年03月23日 13:02:00
 */
public class 面试题链表求和0205 extends BaseLinkedListTest {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int process = 0;
        ListNode result = new ListNode();
        ListNode temp = result;
        while (l1 != null || l2 != null) {
            int num1 = l1 == null ? 0 : l1.val;
            int num2 = l2 == null ? 0 : l2.val;
            int count = num1 + num2 + process;
            process = count / 10;
            int left = count % 10;
            temp.next = new ListNode(left);
            temp = temp.next;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        if (process != 0) {
            temp.next = new ListNode(process);
        }
        return result.next;
    }
}
